﻿using System.Text;
using System.Windows.Media.Imaging;
using System.IO;
using System;
using E_Surfing.WP8Framework.File;

namespace E_Surfing.WP8Framework
{
    /// <summary>
    /// 本地图片类
    /// author:caiwb
    /// data:2011-09-18
    /// </summary>
    public class WPLocalImage : WPFileHandle
    {

        /// <summary>
        /// 图片是否存在
        /// </summary>
        /// <param name="imageName"></param>
        /// <returns></returns>
        public static bool WPImageIsExist(string imageName)
        {
            return FileExist(imageName);
        }

        /// <summary>
        /// 重载：图片是否存在
        /// </summary>
        /// <param name="imageDir"></param>
        /// <param name="imageName"></param>
        /// <returns></returns>
        public static bool WPImageIsExist(string imageDir, string imageName)
        {
            return FileExist(imageDir, imageName);
        }

        /// <summary>
        /// 独立存储获取图片
        /// </summary>
        /// <param name="imageName"></param>
        /// <returns></returns>
        public static BitmapImage WPGetImageFromLocal(string imageName)
        {
            BitmapImage bitmapImage = null;

            try
            {
                if (WPImageIsExist(imageName))
                {
                    using (Stream imgStream = OpenFileForRead(imageName, FileMode.Open))
                    {
                        if (imgStream != null)
                        {
                            bitmapImage = new BitmapImage();
                            bitmapImage.SetSource(imgStream);
                        }
                    }
                }
                return bitmapImage;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static Stream WPGetImageStreamFromLocal(string imageName)
        {
            Stream bitmapStream = null;

            try
            {
                if (WPImageIsExist(imageName))
                {
                    using (Stream imgStream = OpenFileForRead(imageName, FileMode.Open))
                    {
                        if (imgStream != null)
                        {
                            return imgStream;
                        }
                    }
                }
                return bitmapStream;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static Stream WPGetImageStreamFromLocal(string dirName, string imageName)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(dirName);
            sb.Append("\\");
            sb.Append(imageName);

            return WPGetImageStreamFromLocal(sb.ToString());
        }

        /// <summary>
        /// 重载：独立存储获取图片
        /// </summary>
        /// <param name="dirName"></param>
        /// <param name="imageName"></param>
        /// <returns></returns>
        public static BitmapImage WPGetImageFromLocal(string dirName, string imageName)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(dirName);
            sb.Append("\\");
            sb.Append(imageName);

            return WPGetImageFromLocal(sb.ToString());
        }

        /// <summary>
        /// 保存图片到指定的独立存储
        /// </summary>
        /// <param name="imageName"></param>
        /// <param name="imageBytes"></param>
        /// <param name="isCover">当图片存在 是否覆盖</param>
        /// <returns></returns>
        public static bool WPSaveImageToLocal(string imageName, byte[] imageBytes, bool isCover)
        {
            if (FileExist(imageName))
            {
                if (isCover)
                {
                    DeleteFile(imageName);
                    CreateFile(imageName);
                }
                else
                {
                    return true;
                }
            }
            
            //将图片文件存储在独立存储
            return WriteStreamToFile(imageName, imageBytes) == WPResultCode.RESTLT_OK;
        }

        /// <summary>
        /// 保存图片到指定的独立存储
        /// </summary>
        /// <param name="dirName"></param>
        /// <param name="imageName"></param>
        /// <param name="imageBytes"></param>
        /// <param name="isCover">当图片存在 是否覆盖</param>
        public static bool WPSaveImageToLocal(string dirName, string imageName, byte[] imageBytes, bool isCover)
        {
            if (DirectoryExist(dirName))
            {
                if (FileExist(imageName))
                {
                    if (isCover)
                    {
                        DeleteFile(imageName);
                        CreateFile(imageName);
                    }
                    else
                    {
                        return true;
                    }
                }
            }

            return WriteStreamToFile(dirName, imageName, imageBytes) == WPResultCode.RESTLT_OK;
        }

        /// <summary>
        /// 删除独立存储的图片
        /// </summary>
        /// <param name="imageName"></param>
        /// <returns></returns>
        public static bool WPDeleteImage(string imageName)
        {
            string rCode = WPResultCode.RESTLT_OK;

            if (WPImageIsExist(imageName))
            {
                rCode = DeleteFile(imageName);
            }
            return rCode == WPResultCode.RESTLT_OK;
        }

        /// <summary>
        /// 删除独立存储的图片
        /// </summary>
        /// <param name="dirName"></param>
        /// <param name="imageName"></param>
        public static bool WPDeleteImage(string dirName, string imageName)
        {
            string rCode = WPResultCode.RESTLT_OK;

            if (WPImageIsExist(dirName, imageName))
            {
                rCode = DeleteFile(dirName, imageName);
            }
            return rCode == WPResultCode.RESTLT_OK;
        }

        /// <summary>
        /// 删除独立存储的图片目录 包括里面的所有图片
        /// </summary>
        /// <param name="dirName"></param>
        /// <returns></returns>
        public static bool WPDelImageDirectory(string dirName)
        {
            string rCode = WPResultCode.RESTLT_OK;

            if (DirectoryExist(dirName))
            {
                rCode = DeleteDirectory(dirName);
            }
            return rCode == WPResultCode.RESTLT_OK;
        }
    }
}
